<?php
/**
 * tpshop
 * ============================================================================
 * * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.tp-shop.cn
 * ----------------------------------------------------------------------------
 * Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
 * ============================================================================
 * $Author: IT宇宙人 2015-08-10 $
 */
namespace Home\Controller;
use Home\Logic\GoodsLogic;
use Think\AjaxPage;
use Think\Page;
use Home\Logic\UsersLogic;
use Think\Verify;
class GoodsController extends BaseController {


    /**
     * 商品列表页
     */
    public function goodsList(){

      /*  if($_SESSION['user']['user_id']){
            $where_goods['user_id']=array('neq',$_SESSION['user']['user_id']);
        }*/

        // 如果是手机跳转到 手机模块
        if(true == isMobile()){
            header("Location: ".U('Mobile/Goods/goodsList'));
        }


        $where_goods['seller_check']=1;
        $where_goods['status']=1;
        if($is_discount=I('is_discount')){
            $where_goods['is_discount']=$is_discount;
            $this->assign('is_discount', $is_discount);
        }

        $keywords=trim(I('keywords'));
        if($keywords){
            $map['seller_title'] =array('like','%'.$keywords.'%');
            $map['goods_number'] =$keywords;
            $map['_logic'] = 'or';
            $where_goods['_complex'] = $map;
            $this->assign('keywords', $keywords);
//            $where_goods['seller_title']= array('like','%'.$keywords.'%');
//            $where_goods['goods_number']= array('like','%'.$keywords.'%');
        }


        I('type_id')? $type_id=I('type_id'): $type_id=3;




        if($brand_type=I('brand_type')){
            $where_goods['brand_type']=$brand_type;
        }
        if( $trade_type=I('trade_type')){
            $where_goods['trade_type']=$trade_type;
        }
        if($area_type=I('area_type')){
            $where_goods['area_type']=$area_type;
        }
        if($price_type=I('price_type')){
            $where_goods['price_type']=$price_type;
        }
        if($store_type=I('store_type')){
            $where_goods['store_type']=$store_type;
        }

		$credit_type = I('credit_type');
        if($credit_type){
            $credit_arr = explode("_",$credit_type);
            $credit_little =get_credit_little($credit_arr[0]);
            if($type_id==4){
                if($credit_arr[1]=='19'){
                    $where_goods['seller_credit']=array('gt',$credit_little);
                }else{
                    $credit_big=get_credit_big($credit_arr[1]);
                    $where_goods['seller_credit']=array(array('gt',$credit_little),array('lt',$credit_big));
                }
            }
        }



        $type_list=M('goods_type')->where()->select();
        $where_goods['goods_type']=$type_id;
        $order_by=I('sort_asc');
        if(!$order_by){
            $sort_asc='sort DESC,goods_id DESC';
            $this->assign('order', $sort_asc);
        }else{
            $sort_asc='price '.I('sort_asc').',sort DESC,goods_id DESC';
            $this->assign('sort_asc', I('sort_asc'));
        }

        $count=M('goods')->where($where_goods)->count();
        $page = new Page($count,10);


        $goods_list = M('goods')->where($where_goods)->order($sort_asc)->limit($page->firstRow.','.$page->listRows)->select();
        $sales_where['status']=1;
        foreach($goods_list AS $k=>$v){
            $v['sales_qq'] = M('sales')->where($sales_where)->order('rand()')->getField('qq');
            if($v['goods_type']==4){
                $v['tb_xinyong']=get_seller_credit($v['seller_credit']);
            }
            $goods_list[$k]=$v;
        }
        $sales =M('sales')->where($sales_where)->order('rand()')->find();

        if($type_id==3||$type_id==5) {//天猫
            //店铺类型
            $storeType_tm=C('storeType_tm');//天猫
            $this->assign('storeType_tm', $storeType_tm);
            if($store_type)
             $this->assign('xz_store_type', $storeType_tm[$store_type]);

        }
       /* elseif($type_id==5){//京东
            $storeType_jd=C('storeType_jd');//京东
            $this->assign('storeType_jd', $storeType_jd);
            if($store_type)
                $this->assign('xz_store_type', $storeType_jd[$store_type]);
        }*/


        //商标类型
        $brandType=C('brandType');

        //所属行业
        $goodsTrade=C('goodsTrade');

        //所在地区
        $goodsArea=C('goodsArea');

        //价格区间
        $priceType=C('priceType');
        
        //信用等级
        $creditType=C('creditType');
   
        
        
        

		/* M('sales')->where()->find();
		$store_type_name = arrt_name store_type_name;*/

        switch ($type_id)
        {
            case 3:$type_name='天猫商城';break;
            case 4:$type_name='淘宝店铺';break;
            case 5:$type_name='京东商城';break;
            case 6:$type_name='其他商城';break;
            default:$type_name='天猫商城';break;
        }


        $this->assign('type_name', $type_name);


        $this->assign('priceType', $priceType);
        $this->assign('goodsArea', $goodsArea);
        $this->assign('goodsTrade', $goodsTrade);
        $this->assign('brandType', $brandType);
        $this->assign('brand_type',$brand_type);
        $this->assign('trade_type',$trade_type);
        $this->assign('area_type',$area_type);
        $this->assign('price_type',$price_type);
        $this->assign('store_type',$store_type);
		$this->assign('creditType',$creditType);
		$this->assign('credit_type',$credit_type);
		$this->assign('credit_type1',$credit_arr[0]);
		$this->assign('credit_type2',$credit_arr[1]);
//		$this->assign('store_type_name',$store_type_name);


        $this->assign('page',$page->show());// 赋值分页输出
        $this->assign('sales',$sales);
        $this->assign('lists',$goods_list);
        $this->assign('type_list',$type_list);
        $this->assign('type_id',$type_id);
//        $this->assign('sort_asc',$sort_asc);
        $this->display();


    }


    /**(kenvid)
     * 商品详情页
     */
    public function goodsInfo(){
        $goods_id = I("get.id");

        // 如果是手机跳转到 手机模块
        if(true == isMobile()){
            header("Location: ".U('Mobile/Goods/goodsInfo',array('id'=>$goods_id)));
        }


        //记住浏览记录
        $look_arr=cookie('look_arr');
        if(!in_array($goods_id,$look_arr))
                    $look_arr[]=$goods_id;
        cookie('look_arr',$look_arr);


        //  form表单提交
        C('TOKEN_ON',true);
        $goodsLogic = new \Home\Logic\GoodsLogic();

        $goods = M('Goods')->where("goods_id = $goods_id")->find();

        if(!$goods)
          $this->error('商品不存在！',U('goodsList'));
        if($goods['status']!=1)
            $this->error('商品已下架！',U('goodsList'));

        if($goods['goods_type']==4){
            $goods['tb_xinyong']=get_seller_credit($goods['seller_credit']);
        }

        $user_id= $_SESSION['user']['user_id'];
        $c_where['user_id']=$user_id;
        $c_where['goods_id']=$goods_id;
        $count = M('goods_collect')->where($c_where)->count();
        if($count){
            $is_collect=1;
        }else{
            $is_collect=0;
        }

        //随机获取业务员数据   （3条）
         $sales_where['status']=1;
         $sales_list=M('sales')->where($sales_where)->order('rand()')->limit(3)->select();

        //违规扣分数组
        $weigui_log =explode('-',$goods['weigui_log']);

        /*//推荐数据  2条
        $tj_where['status']=1;
        $tj_where['is_recommend']=1;
        $tj_where['goods_id']=array('neq',$goods['goods_id']);
        $tj_list=M('goods')->where($tj_where)->order('rand()')->limit(2)->select();*/



        //推荐数据  2条
        $tj_where['status']=1;
        $tj_where['goods_type']=$goods['goods_type'];//同类型
        $tj_where['trade_type']=$goods['trade_type'];//同行业
        $tj_where['area_type']=$goods['area_type'];//同地区
        $tj_where['goods_id']=array('neq',$goods['goods_id']);
        $tj_list=M('goods')->where($tj_where)->order('rand()')->limit(2)->select();
        if(count($tj_list)<2){
            $tj1_where['status']=1;
            $tj1_where['is_recommend']=1;
            $tj1_where['goods_id']=array('neq',$goods['goods_id']);
            $tj_list=M('goods')->where($tj1_where)->order('rand()')->limit(2)->select();
        }


        //查看过该店铺的用户还看了  6条（热门）
        $rm_where['status']=1;
        $rm_where['is_hot']=1;
        $rm_where['goods_id']=array('neq',$goods['goods_id']);
        $rm_list=M('goods')->where($tj_where)->order('rand()')->limit(6)->select();




            //店铺类型
            $storeType_tm=C('storeType_tm');//天猫
            $this->assign('storeType_tm', $storeType_tm);

            $storeType_jd=C('storeType_jd');//京东
            $this->assign('storeType_jd', $storeType_jd);



        //商标类型
        $brandType=C('brandType');
        $this->assign('brandType', $brandType);


        //所属行业
        $goodsTrade=C('goodsTrade');
        $this->assign('goodsTrade', $goodsTrade);

        //所在地区
        $goodsArea=C('goodsArea');
        $this->assign('goodsArea', $goodsArea);

        //价格区间

        $priceType=C('priceType');
        $this->assign('priceType', $priceType);



        $this->assign('tj_list',$tj_list);
        $this->assign('rm_list',$rm_list);
        $this->assign('sales_list',$sales_list);
        $this->assign('weigui_log',$weigui_log);
        $this->assign('is_collect',$is_collect);
        $this->assign('goods',$goods);
    /*    dump($goods);*/
        $this->assign('user_id',$_SESSION['user']['user_id']);
        $this->display();
    }

    /**
     * 添加订单
     */
    public function add_order(){
        $user_id =$_SESSION['user']['user_id'];
        if(!$user_id)
            $this->error('请登录后方可购买，请登录！',U('Home/User/login'));

        $user=M('users')->where(array('user_id'=>$user_id))->find();
        if(!$user['mobile']){
            $this->error('需要绑定手机才可可进行交易，请绑定！',U('Home/User/mobile_validate',array('step'=>1)));
        }

        $goods_id =I('goods_id');
        $where_o1['user_id']=$user_id;
        $where_o1['goods_id']=$goods_id;
        $where_o1['order_status']=array('neq','2,4');
        $order =M('order1')->where($where_o1)->find();
//        $order =M('order1')->where(array('user_id'=>$user_id,'goods_id'=>$goods_id))->find();

        if($order&&$order['pay_status']==1){
            if($order['order_status']==1||$order['order_status']==2||$order['order_status']==6||$order['order_status']==8){
                $this->error('该商品已完成支付',U('User/order_detail',array('id'=>$order['order_id'])));
            }
        }
        if($order&&$order['pay_status']==0&&$order['is_delete']==0){
            $this->error('该店铺已经在您的待付款订单里,请即时支付！',U('User/order_pay',array('order_id'=>$order['order_id'])));
        }

        $s_where['goods_id']=$goods_id;
        $goods=M('goods')->where($s_where)->find();
        if($goods['user_id']==$user_id)
            $this->error('不能购买自己的店铺');
        if(!$goods)
            $this->error('店铺不存在');
        if($goods['status']!=1)
            $this->error('店铺已下架');
        //订单总价 = 手续费+店铺价格+保证金+技术年费

        $price=$goods['price'];//售价
        $sxf_ratio=M('config')->where("name='sxf_ratio'")->getField('value');//手续费比例
        $yfk_ratio=M('config')->where("name='yfk_ratio'")->getField('value');//预付款比例
        $yfk_top=M('config')->where("name='yfk_top'")->getField('value');// 预付款上限
        if(!$sxf_ratio)
            $sxf_ratio=10;
        if(!$yfk_ratio)
            $yfk_ratio=20;
        $j_price=$goods['j_price'];//技术年费
        $b_price=$goods['b_price'];//保证金
        if($goods['goods_type']==3){ //天猫 手续费=店铺价格*10%
            $s_price=$price*$sxf_ratio/100;//
        }else{ //其他 //手续费 =（店铺价格+技术年费+保证金）*10%
            $s_price=($price+$j_price+$b_price)*$sxf_ratio/100;
        }

        $total_amount=$price+$j_price+$b_price+$s_price;
        $pay_price=($price+$j_price+$b_price+$s_price)*$yfk_ratio/100;// 当前订单需要支付的金额=（店铺价格+技术年费+保证金+手续费）*10%
        if($pay_price<0.01)
            $pay_price=0.01;
        if($yfk_top>0&&$pay_price>$yfk_top)//应支付金额大于上限制变成上限金额
            $pay_price=$yfk_top;

        $add_data['goods_id']=I('goods_id');
        $add_data['user_id']=$user_id;
        $add_data['order_sn']=date('YmdHis').rand(1000,9999);
        $add_data['add_time']=time();
        $add_data['total_amount']= $total_amount;
        $add_data['price']=$price;
        $add_data['b_price']=$b_price;
        $add_data['j_price']=$j_price;
        $add_data['s_price']=$s_price;
        $add_data['pay_price']=$pay_price;
        $order_id=M('order1')->add($add_data);
        if($order_id){
            $this->error('添加订单成功,请及时支付！',U('User/order_pay',array('order_id'=>$order_id)));
        }else{
            $this->error('添加订单不成功，请重试！',U('goodsInfo',array('id'=>$goods_id)));
        }
    }

    /**
     * 用户收藏某一件商品
     * @param type $goods_id
     */
    public function collect_goods($goods_id)
    {
        $goods_id = I('goods_id');
        $goodsLogic = new \Home\Logic\GoodsLogic();
        $result = $goodsLogic->collect_goods($_SESSION['user']['user_id'],$goods_id);
        exit(json_encode($result));
    }



    /**
     * 用户收藏某一件商品
     * @param type $goods_id
     */
    public function q_collect_goods($goods_id)
    {
        $goods_id = I('goods_id');
        $goodsLogic = new \Home\Logic\GoodsLogic();
        $result = $goodsLogic->collect_goods($_SESSION['user']['user_id'],$goods_id);
        exit(json_encode($result));
    }


    public function cancel_collect()
    {
        $goods_id=I('goods_id');
        $where['goods_id']=$goods_id;
        $where['user_id']=$_SESSION['user']['user_id'];
        $res=M('goods_collect')->where($where)->delete();

    }





}